package com.cqs.leetcode;

import java.util.Arrays;
import java.util.Random;
import java.util.stream.Collectors;

public class ListNode {
    public int val;
    public ListNode next;

    public ListNode(int x) {
        val = x;
    }

    public static ListNode build(int[] nums) {
        ListNode head = null;
        ListNode pre = null;
        for (int i = 0; i < nums.length; i++) {
            ListNode node = new ListNode(nums[i]);
            if (head == null) {
                head = node;
                pre = node;
            } else {
                pre.next = node;
                pre = node;
            }
        }
        return head;
    }

//    @Override
//    public String toString() {
//        StringBuilder sb = new StringBuilder("[");
//        while (node != null) {
//            sb.append(node.val + "->");
//            node = node.next;
//        }
//        if (sb.length() > 1) {
//            sb.delete(sb.length() - 2, sb.length());
//        }
//        sb.append("]");
//        return sb.toString();
//    }


    @Override
    public String toString() {
        ListNode node = this;
        StringBuilder sb = new StringBuilder("[");
        while (node != null) {
            sb.append(node.val + "->");
            node = node.next;
        }
        if (sb.length() > 1) sb.delete(sb.length() - 2, sb.length());
        sb.append("]");
        return sb.toString();
    }


//    @Override
//    public String toString() {
//        return val+"";
//    }


    public static void main(String[] args) {
        int nums = 10;
        int[] arrs = new int[nums];
        for (int i = 0; i < arrs.length; i++) {
            arrs[i] = new Random().nextInt(nums);
        }
        System.out.println(Arrays.stream(arrs).boxed().collect(Collectors.toList()));


        System.out.println(randomLink(5));
    }

    public static ListNode randomLink(int n){
        Random random = new Random();
        int bound = n * 2;
        ListNode root = new ListNode(random.nextInt(bound));
        ListNode pre = root;
        for (int i = 0; i < n - 1; i++) {
            ListNode node = new ListNode(random.nextInt(bound));
            pre.next = node;
            pre = node;
        }
        return root;
    }

}
